package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    // @Autowired
    // private OrderDetailMapper orderDetailMapper;

    /**
     * 取消订单业务逻辑
     * @param cancelDTO
     */
    @Override
    public void cancelOrder(OrdersCancelDTO cancelDTO) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(cancelDTO, orders);
        orderMapper.update(orders);

        //TODO 后面详细优化
    }

    /**
     * 各个状态的订单数量统计业务逻辑
     * @param
     * @return
     */
    @Override
    public OrderStatisticsVO getOrderStatistics() {
        //1.根据状态，分别查询待接单、带派送、派送中的订单数量
        Integer toBeConfirmed = orderMapper.countStatus(Orders.TO_BE_CONFIRMED); //待接单
        Integer confirmed = orderMapper.countStatus(Orders.CONFIRMED); //3已接单 - 待派送
        Integer deleveryInProgress = orderMapper.countStatus(Orders.DELIVERY_IN_PROGRESS); //派送中
        //2.将查询出的数据封装到orderStatisticsVO中响应
        OrderStatisticsVO statisticsVO = new OrderStatisticsVO();
        statisticsVO.setToBeConfirmed(toBeConfirmed);
        statisticsVO.setConfirmed(confirmed);
        statisticsVO.setDeliveryInProgress(deleveryInProgress);
        return statisticsVO;
    }

    /**
     * 订单搜索业务逻辑
     * @param ordersPageQueryDTO
     * @return
     */
    @Override
    public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        //执行分页查询
        Page<Orders> page = orderMapper.pageQuery(ordersPageQueryDTO);

        //-*-部分订单状态，需要额外返回订单菜品信息，将Orders转化为OrderVo
        List<OrderVO> orderVOList = getOrderVOList(page);
        return new PageResult(page.getTotal(), orderVOList);
    }

    private List<OrderVO> getOrderVOList(Page<Orders> page) {
        //将返回的订单菜品信息自定义到OrderVO响应结果
        List<OrderVO> orderVOList = new ArrayList<>();
        List<Orders> ordersList = page.getResult();
        //判断订单信息是否为空
        if (!CollectionUtils.isEmpty(ordersList)) {
            //循环遍历订单信息
            for (Orders orders : ordersList) {
                //将共同字段复制到OrderVO
                OrderVO orderVO = new OrderVO();
                BeanUtils.copyProperties(orders, orderVO);
                String orderDishes = getOrderDishesStr(orders);

                //封装到orderVO，添加到orderVOList
                orderVO.setOrderDishes(orderDishes);
                orderVOList.add(orderVO);
            }
        }
        return orderVOList;
    }

    private String getOrderDishesStr(Orders orders) {
        //查询订单菜品详情信息
        return null;
    }
}
